{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:44:54.698957Z",
     "start_time": "2019-02-13T10:44:54.695958Z"
    }
   },
   "outputs": [],
   "source": [
    "file = r\"D:/_DATA/CASIA-FaceV5/CASIA-FaceV5 (000-099)/000/000_1.bmp\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 提取边缘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:45:02.589953Z",
     "start_time": "2019-02-13T10:45:01.934955Z"
    }
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:45:02.610953Z",
     "start_time": "2019-02-13T10:45:02.591953Z"
    }
   },
   "outputs": [],
   "source": [
    "# file = r\"G:/_repos/pix2pix-tensorflow/photos/resized/0.png\"\n",
    "img = cv2.imread(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:47:59.923168Z",
     "start_time": "2019-02-13T10:47:59.919154Z"
    }
   },
   "outputs": [],
   "source": [
    "img = np.array(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:46:42.438906Z",
     "start_time": "2019-02-13T10:46:42.434904Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(256, 256, 3)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:48:01.776155Z",
     "start_time": "2019-02-13T10:48:01.773154Z"
    }
   },
   "outputs": [],
   "source": [
    "gaussian = cv2.GaussianBlur(src=img, ksize=(11, 11), sigmaX=0, sigmaY=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:48:01.948155Z",
     "start_time": "2019-02-13T10:48:01.943154Z"
    }
   },
   "outputs": [],
   "source": [
    "canny = cv2.Canny(gaussian, 50, 90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:48:02.264170Z",
     "start_time": "2019-02-13T10:48:02.256156Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cv2.imwrite(r\"G:/_repos/pix2pix-tensorflow/photos/1.png\",canny)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:48:04.943155Z",
     "start_time": "2019-02-13T10:48:02.696154Z"
    }
   },
   "outputs": [],
   "source": [
    "cv2.imshow(\"origin\", img)\n",
    "cv2.imshow(\"gaussian\",gaussian)\n",
    "cv2.imshow(\"canny\", canny)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 裁剪\n",
    "直接中心裁剪一个256*256的框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:36:01.793419Z",
     "start_time": "2019-02-13T10:36:01.773418Z"
    }
   },
   "outputs": [],
   "source": [
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:47:48.928156Z",
     "start_time": "2019-02-13T10:47:48.919155Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None (640, 480) RGB\n"
     ]
    }
   ],
   "source": [
    "\n",
    "im = Image.open(file)\n",
    "print(im.format, im.size, im.mode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:47:55.283154Z",
     "start_time": "2019-02-13T10:47:55.278155Z"
    }
   },
   "outputs": [],
   "source": [
    "new_width = new_height = 256\n",
    "\n",
    "width, height = im.size   # Get dimensions\n",
    "\n",
    "left = (width - new_width)/2\n",
    "top = (height - new_height)/2\n",
    "right = (width + new_width)/2\n",
    "bottom = (height + new_height)/2\n",
    "\n",
    "im = im.crop((left, top, right, bottom))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-02-13T10:55:18.380955Z",
     "start_time": "2019-02-13T10:55:18.334959Z"
    }
   },
   "outputs": [],
   "source": [
    "im.save('123.jpg')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
